﻿using System.Collections.Generic;
using System.Linq;
using compshop.Infrastructure.CompShopDataService;
using compshop.Infrastructure;
using compshop.Modules.OrdersToSupplier.Views;
using System.Windows;

namespace compshop.Modules.OrdersToSupplier.Presenter
{
    public class OrdersToSupplierPresenter
    {
        private readonly CompShopEntities contextDS;
        private EventsManager eventsManager;

        private IList<OrderToSupplier> orderToSupplierList;
    
        public IViewOrdersToSupplier View { get; set; }

        public IList<OrderToSupplier> OrderToSupplierList
        {
            get { return orderToSupplierList; }
            set { orderToSupplierList = value; }
        }

        public IList<Supplier> SupplierList { get; set; }

        public IList<SupplierGeneralProductList> GeneralProductList { get; set; }

        public IList<Employee> EmployeeList { get; set; }
    
        public OrdersToSupplierPresenter(EventsManager eventsManager)
        {
            contextDS = CompShopEntitiesContext.GetInstance();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();

            SupplierList=contextDS.Suppliers.ToList<Supplier>();
            GeneralProductList = contextDS.SupplierGeneralProductLists.ToList<SupplierGeneralProductList>();
            EmployeeList = contextDS.Employees.ToList<Employee>();

            LoadOrderToSupplier();
        }


        private void LoadOrderToSupplier()
        {
            orderToSupplierList = contextDS.OrderToSuppliers.
                Expand("Supplier,Employee").ToList<OrderToSupplier>();
            foreach (OrderToSupplier ord in orderToSupplierList)
            {
                ord.SupplierGeneralProductLists.Load(contextDS.SupplierGeneralProductLists.Where(x=> x.OrderToSupplierID==ord.OrderToSupplierID));
                foreach (SupplierGeneralProductList gpl in ord.SupplierGeneralProductLists)
                {
                    gpl.GeneralProduct = contextDS.GeneralProducts.Where(x => x.GeneralProductID == gpl.GeneralProductID).First();
                    contextDS.LoadProperty(gpl.GeneralProduct, "Producer");
                }
            }
        }


        public void ShowCreateOrderToSupplierDialog()
        {
            OrderToSupplier order = new OrderToSupplier();
            CreateOrderToSupplierDialog dialog = new CreateOrderToSupplierDialog(order);
            dialog.Presenter = this;
            if (dialog.ShowDialog() == true)
            {
                contextDS.AddToOrderToSuppliers(order);
                //contextDS.SaveChanges();
                //LoadOrderToSupplier();
                //View.Presenter = this;

                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
                else
                {
                    LoadOrderToSupplier();
                    View.Presenter = this;
                }
            }
        }


        public void ShowEditOrderToSupplierDialog(OrderToSupplier order)
        {
            CreateOrderToSupplierDialog dialog = new CreateOrderToSupplierDialog(order);
            dialog.Presenter = this;
            if (dialog.ShowDialog() == true)
            {
                contextDS.UpdateObject(order);
                if (CompShopEntitiesContext.Save() < 0)
                    MessageBox.Show(Resource.ErrorSaveChanges);
                else
                {
                    LoadOrderToSupplier();
                    View.Presenter = this;
                }
            }
        }


        #region Publishers

        public void OrderToSupplierSelectedPublisher(int id)
        {
            eventsManager.eventAggregator.GetEvent<OrderToSupplierSelectedEvent>().Publish(id);
        }

        #endregion


        #region Handlers

        public void OrderToSupplierSelectedEventHandler(int id)
        {
        }

        #endregion
    }
}
